<?php
/**
 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;


use app\common\controller\AdminController;

use app\admin\service\NodeService;
use think\App;
use think\Model;


/**
 * @ControllerAnnotation(title="代理用户管理")
 * Class Node
 *
 * @package app\admin\controller\system
 */
class User extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 允许修改的字段
     *
     * @var array
     */
    protected $allowModifyFields = [
        'is_freeze'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\User();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $t = time();
        $request = $this->request;

        $page = input("page");
        $limit = input("limit");
        
        $where = [];
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (! empty(input('pid'))) {
            $where[] = [
                "figure_user.pid",
                "=",
                input('pid')
            ];
        }

        if (! empty(input('nickname'))) {
            $where[] = [
                "nickname",
                "LIKE",
                "%" . input('nickname') . "%"
            ];
        }
        if (! empty(input('telphone'))) {
            $where[] = [
                "telphone",
                "LIKE",
                "%" . input('telphone') . "%"
            ];
        }
        if (! empty(input('start_time'))) {
            $where[] = [
                "user.create_time",
                ">=",
                strtotime(input('start_time'))
            ];
        }
        
        if (! empty(input('end_time'))) {
            $where[] = [
                "create_time",
                "<=",
                strtotime(input('end_time'))
            ];
        }

        if (! empty(input('platform_name'))) {
            $where[] = [
                "platformInfo.name",
                "=",
               input('platform_name')
            ];
        }
             $count = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    /**
     * 重置密码
     * @NodeAnotation(title="重置密码")
     */
    public function resetPassword()
    {
        $request = $this->request;
        try {
            $password = md5("123456");
            $id = input("id");
            $row = $this->model->where("id", $id)->find();
            if (empty($row)) {
                return msg([], 1, "数据不存在");
            }
            $row->password = $password;
            $row->save();
            return msg([], 0, "重置成功");
        } catch (\Exception $th) {
            return msg([], 1, "重置异常 : " . $th->getMessage());
        }
    }

    /**
     * 重新生成二维码
     */
    public function reloadQrcode()
    {
        $request = $this->request;
        $user_id = $this->request->_UID;
        $id = input('id');
        $row = $this->model->find($id);
        if (empty($row)) {
            return msg([], 1, "数据不存在");
        }
        
        try {
            
            $url = "pages/index/index";
            $userUrl = qr_code($row['id'], $url, 'user');
            
            $save = $row->save([
                'qrcode' => $userUrl
            ]);
        } catch (\Exception $e) {
            return msg([], 1, "操作异常" . $e->getMessage());
        }
        return $save ? msg([], 0, "操作成功") : msg([], 1, "操作失败");
    }

    public function delete()
    {

        $request = $this->request;
        $param = $request->param();
        $this->model->whereIn('id', $param['id'])->update(['is_delete' => 1]);

        return msg([

        ], 0, "删除成功");
    }


}